<%@page import="java.sql.*"%>
<%@page import="com.mysql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr" >
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Rentflix Movie Search</title>

<!-- <link rel="stylesheet" href="../../css/style.css"> -->
<link rel="stylesheet" href="resources/mytheme/css/normalize.css">
<link rel="stylesheet" href="resources/mytheme/css/stylesheet.css">

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="jquery.jcarousel.pack.js"></script>
<script type="text/javascript" src="jquery.easing.1.3.js"></script>
<script type="text/javascript" src="jquery.jcarousel.setup.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="jquery.touchslider.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
//redirects to users queue
function toQ()
{
	window.location.href = "/MovieProject/viewqueue";
}

//Only run everything once the page has completely loaded
$(window).load(function(){

    // Set general variables
    // ====================================================================
    var totalWidth = 0;

    // Total width is calculated by looping through each gallery item and
    // adding up each width and storing that in `totalWidth`
    $(".gallery__item").each(function(){
        totalWidth = totalWidth + $(this).outerWidth(true);
    });

    // The maxScrollPosition is the furthest point the items should
    // ever scroll to. We always want the viewport to be full of images.
    var maxScrollPosition = totalWidth - $(".gallery-wrap").outerWidth();

    // This is the core function that animates to the target item
    // ====================================================================
    function toGalleryItem($targetItem){
        // Make sure the target item exists, otherwise do nothing
        if($targetItem.length){

            // The new position is just to the left of the targetItem
            var newPosition = $targetItem.position().left;

            // If the new position isn't greater than the maximum width
            if(newPosition <= maxScrollPosition){

                // Add active class to the target item
                $targetItem.addClass("gallery__item--active");

                // Remove the Active class from all other items
                $targetItem.siblings().removeClass("gallery__item--active");

                // Animate .gallery element to the correct left position.
                $(".gallery").animate({
                    left : - newPosition
                });
            } else {
                // Animate .gallery element to the correct left position.
                $(".gallery").animate({
                    left : - maxScrollPosition
                });
            };
        };
    };

    // Basic HTML manipulation
    // ====================================================================
    // Set the gallery width to the totalWidth. This allows all items to
    // be on one line.
    $(".gallery").width(totalWidth);

    // Add active class to the first gallery item
    $(".gallery__item:first").addClass("gallery__item--active");

    // When the prev button is clicked
    // ====================================================================
   
    $(".gallery__controls-prev").click(function(){
        
        // Set target item to the item before the active item
    
        var $targetItem = $(".gallery__item--active").prev();
        
        toGalleryItem($targetItem);
        
    });

    // When the next button is clicked
    // ====================================================================
    $(".gallery__controls-next").click(function(){
        // Set target item to the item after the active item
        var $targetItem = $(".gallery__item--active").next();
        toGalleryItem($targetItem);
    });
});
</script>
<script type="text/javascript" src="./resources/fancybox/jquery.fancybox-1.3.1.js"></script>
<script type="text/javascript">
// Fancybox specific
// To make images pretty. Not important
$(document).ready(function(){
    $(".gallery__link").fancybox({
        'titleShow'     : false,
        'transitionIn'  : 'elastic',
        'transitionOut' : 'elastic'
    });
});
</script>

</head>
<body>
	<%
    try{


 String searchFor = request.getParameter("searchBox");
 String showDescr = request.getParameter("desc");
 String sqlQuery = "select * from movies";
 String reviewQuery = "select * from reviews where movieid = (select movieid from movies where title like '" + searchFor + "')";
 String reviewQuery2 = "select review,score,firstname from reviews join users on users._z4 = reviews.userid where movieid = (select movieid from movies where title like '" + searchFor + "')";
 String commentURL = "/MovieProject/addcomment?searchBox=" + searchFor;
 if(searchFor != null)
 {
	 sqlQuery += " where title like '%" + searchFor +"%'";
 }
Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

       Statement statement = connection.createStatement();
       Statement statement2 = connection.createStatement();
       Statement statement4 = connection.createStatement() ;
       Statement statement6 = connection.createStatement() ;
       Statement statement5 = connection.createStatement();
       ResultSet rs = statement.executeQuery(sqlQuery);
       ResultSet qs = statement2.executeQuery(reviewQuery2);

%>
<div id=header>

	<div id="logo">
		<img src="resources/mytheme/img/Logo.png" alt="RentFlix">
	</div>
	
	<div id="searchNav">
		<form name="inputForm" method="get">
			<input class="searchBox" type="text" name="searchBox" placeholder="Search for Movies..." onkeyup="fastSearch()">
			<input class="searchButton" type="submit" value="Search">
			<br>
		</form>
	</div>
	
	<div id="navBar">
		<ul>
		    <li><a href="/MovieProject/browse">Movies</a></li>
		    <li><a href="/MovieProject/browse?genre=Family&rating=&decade=">Kids</a></li>
		    <li><a href="/MovieProject/viewqueue">Queue</a></li>
		</ul>
	</div>
	
	<div id="loginBar">
		<ul>

			<li><div id="signoutform"><form action="signout" method="post" name="loutform"><input class="lastlink" type="submit" value="Sign Out" style="text-transform:capitalize;"></form></div></li>
			<li><a class="firstlink"href="/MovieProject/MyAccount">My Account</a></li>
		</ul>
	</div>
	
</div>    
	<div id="body">
	
	 <% while(rs.next()){  %>
	 
		<h1 id="MovieTitle"><%= rs.getString(2)%></h1>
		
		<div id="image">
			<img src="<%= rs.getString(8)%>" alt="some_text">
		</div>
		<div id="MovieInfo">
			<ul>
				<li><%= rs.getString(3)%></li>
				<li><%= rs.getString(4)%></li>
				<li><%= rs.getString(5)%></li>
				<li><%= rs.getString(7)%></li>
			</ul>
		</div>
		<div id="Description">
			<p><%= rs.getString(6)%></p>
		</div>
        <% } %>
        
        <h3 class="ReviewTitle">User Reviews</h3>
			
			<div id="ReviewTable">
				<div id="ReviewHeader">
					<ul>
						<li>User</li>
						<li>Score</li>
						<li>Comment</li>
					</ul>
				</div>
				
			   <% while(qs.next()){  %>
			   
				<div id="UserContent">
					<ul>
						<li><div class="user"><%= qs.getString(3)%></div></li>
						<li><div class="score"><%= qs.getString(2)%></div></li>
						<li><div class="comment"><p><%= qs.getString(1)%></p></div></li>
					</ul>
				</div>
				
			<% } %>
		</div>

<%
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>
<%
    try{
    	int u = (Integer)request.getSession().getAttribute("uName");
    	int i = 4;
    	
    	String searchFor = request.getParameter("searchBox");
		String movie = searchFor;
   
		Connection connection = 
         DriverManager.getConnection
            ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

       Statement statement = connection.createStatement() ;
       Statement statement2 = connection.createStatement() ;
       Statement statement3 = connection.createStatement();
       Statement statement5 = connection.createStatement();
       Statement statement6 = connection.createStatement();
       ResultSet rs6 = statement6.executeQuery("select * from movies where title = '" + searchFor + "'") ;  
       ResultSet rs5 = statement5.executeQuery("select * from movies JOIN queue ON movies.MovieID = queue.MovieID JOIN users ON users._z4 = queue.UserID WHERE queue.UserID ="+u+";") ;  
       boolean inQ = false;
       rs6.next();
       while(rs5.next())
       {
    	   if(rs6.getString(1).equals(rs5.getString(1)))
    	   {
    		   inQ = true;
    	   }
       }
       
       ResultSet rs = statement.executeQuery("select * from movies WHERE Title='"+searchFor+"'") ;
       ResultSet rs3 = null;
           rs3 = statement3.executeQuery("SELECT (MAX(queue.Position)+1) as Maxp From queue INNER JOIN users ON queue.UserID = users._z4 WHERE users._z4 ="+u); 
       String s = "0";
       if(rs3.next()){ 
       		s = rs3.getString(1); 
       }   
   
%>   

<%if(inQ){%>
	   
	   <div id="Queue_Style">
       <form:form action="movie" method="post" commandName="queueForm">
               <% while(rs.next()){ %>
               <ul>
	           <% } %> 
	           <li><div class="queue_button"><input type="button" value="Already in Queue" onclick="toQ()"/></div></li>
           	</ul>
       </form:form>
   </div>
	   
<% } else{ %>
        <div id="Queue_Style">
        <form:form action="movie" method="post" commandName="queueForm">
            
                <% while(rs.next()){ %>
                <ul>
	                <li><form:hidden path="movieID" value="<%=rs.getString(1)%>" readonly="true"/></li>
	                  <% } %>
	                <li><form:hidden path="userID" value="<%=u%>" readonly="true"/></li>
	                <li><form:hidden path="position" value="<%=s%>" readonly="true"/></li>
	                <li><div class="queue_button"><input type="submit" value="Add to Queue"/></div></li>
            	</ul>
        </form:form>
    </div>

<% } %>
<%
//**Should I input the codes here?**
        }
        catch(Exception e)
        {
             out.println("wrong entry"+e);
        }
%>
<% try{
	 String a = request.getParameter("searchBox");
	  Connection connection = 
	         DriverManager.getConnection
	           ("jdbc:mysql://localhost:3306/moviedatabase","dbuser","dbpassword");

	       Statement statement2 = connection.createStatement() ;
	   
	       //selects all movies that have the same actor, or the same genre
	       ResultSet rs2 = statement2.executeQuery("select * from movies where (genre = (select genre from movies where title = '"+ a +"')) or actors = (select actors from movies where title = '"+ a +"')") ;  
	      
	 %>
	 
	<div id="ReviewMovie">
		<a href="/MovieProject/addcomment?searchBox=<%= a %> ">Review This movie</a>
	</div>
	<div id="content">
	<center> <h1>Movies You Might Also Like</h1></center>
<div class="gallery-wrap">
  <div class="gallery clearfix">
     <% while(rs2.next()){ %>
    <div class="gallery__item">
    
  <a href="/MovieProject/movie?searchBox=<%= rs2.getString(2)%>" >
      <img src="<%=rs2.getString(8)%>" class="gallery__img" alt=""  /></a>

    </div>
         <% } %>
  </div>
  <div class="gallery__controls clearfix">
    <div href="#" class="gallery__controls-prev">
      <img src="resources/movieImages/prev.png" alt=""/>
    </div>
    <div href="#" class="gallery__controls-next">
      <img src="resources/movieImages/next.png" alt="" />
    </div>
  </div>
</div>
</div>
	
<%}
catch(Exception e)
{
     out.println("wrong entry"+e);
}
%>
	</div>
		<div id="footer">
			<div id="foota">
<p>Get Help</p>
<ul>
  <li><a href="#">FAQ</a></li>
  <li><a href="#">Help Center</a></li>
  <li><a href="#">Account</a></li>
  <li><a href="#">Questions</a></li>
</ul>


</div>
<div id="footb"><p>Our Company</p>
<ul>
  <li><a href="#">About us </a></li>
  <li><a href="#">Reviews  </a></li>
  <li><a href="#">Jobs  </a></li>
  <li><a href="#">Terms of Use</a></li>
</ul>

</div>
<div id="footc"><p>Getting Started</p>
<ul>
  <li><a href="#">Free Trail Offer Details  </a></li>
  <li><a href="#"> Redeem Your Gift</a></li>
</ul>

</div>
		</div>
</body>
</html>